package com.webcamtracker.ui;

import com.webcamtracker.video.VideoManager;
import com.webcamtracker.video.VideoSuscriptor;
import com.webcamtracker.video.codecs.ImageProviderCodec;

import javax.media.CaptureDeviceInfo;
import javax.media.Codec;
import javax.media.format.VideoFormat;
import javax.swing.*;
import java.awt.*;

/**
 * Manage Video showing
 */
public class UIVideoMonitor
        extends JPanel {
    //~ Instance fields ......................................................................................

    //    private VideoPollingService service;
    private VideoManager videoManager = null;
    private ImageProviderCodec imageProviderCodec;
    private JPanel video;
    private VideoListener[] listener;



    public UIVideoMonitor(VideoListener... listener) {
        super(new BorderLayout(), true);
        this.listener = listener;
        configure();
        configureUI();
        video = new JPanel();
    }



    public void addVideoSuscriptor(VideoSuscriptor videoSuscriptor) {

        imageProviderCodec.addVideoSuscriptor(videoSuscriptor);
    }

    public void startVideo(String url) {
        imageProviderCodec.setType(ImageProviderCodec.Type.Video);
        videoManager.startVideo(url);
        final VideoFormat format = videoManager.getVideoFormat();
        this.setSize(format.getSize());
        video.add(videoManager.getVideoComponent());
        add(video, BorderLayout.CENTER);
        add(videoManager.getProcessor().getControlPanelComponent(), BorderLayout.SOUTH);
        repaint();
        for (VideoListener videoListener : listener) {
            videoListener.videoStarted(format);
        }
    }

    public void startWebCam(CaptureDeviceInfo info, VideoFormat format) {
        imageProviderCodec.setType(ImageProviderCodec.Type.WebCam);
        videoManager.startWebCam(info, format);
        this.setSize(format.getSize());
        video.add(videoManager.getVideoComponent());
        add(video, BorderLayout.CENTER);
        add(videoManager.getProcessor().getControlPanelComponent(), BorderLayout.SOUTH);
        repaint();
        for (VideoListener videoListener : listener) {
            videoListener.videoStarted(format);
        }
    }

    public void stopVideo() {
        if (videoManager.isStarted()) {
            video.remove(videoManager.getVideoComponent());
            remove(videoManager.getProcessor().getControlPanelComponent());
            repaint();
            videoManager.stopVideo();
        }
        for (VideoListener videoListener : listener) {
            videoListener.videoStopped();
        }

//        service.stopService();

    }

    public VideoManager getVideoManager() {
        return videoManager;
    }

    private void configure() {
        imageProviderCodec = new ImageProviderCodec();
        videoManager = new VideoManager(new Codec[]{imageProviderCodec});
        imageProviderCodec.setManager(videoManager);
//        service = new VideoPollingService(videoManager);
    }

    private void configureUI() {
        setBorder(BorderFactory.createTitledBorder("Monitor"));
    }
}
